package com.job.ZhangMeng.Homework0715;

public class StringCompare {

    public static void main(String[] args) {
        String name1="lucy",name2="luck";
        System.out.println(name1.compareTo(name2)); //'y'-'k'
        System.out.println("luc".compareTo(name2)); //缺少字符
        System.out.println("luck".compareTo(name2));//相同
        //本质上,compareTo()方法是String类实现java.lang.Comparable接口的方法
        //同一类型的对象要想有可比性,需实现并重写该接口

        String  s1=new String(new char[]{'A','6','?'});
        s1=s1+name1;    //连接方式1
        System.out.println(s1);
        s1=s1.concat("2022");   //连接方式2
        System.out.println(s1);

        String url="https://www.baidu.com/s?wd=%E6%96%B0%E7%96%86%E4%B8%8D%E5%86%8D%E6%98%AF%E8%BE%B9%E8%BF%9C%E5%9C%B0%E5%B8%A6&sa=fyb_n_homepage&rsv_dl=fyb_n_homepage&from=super&cl=3&tn=baidutop10&fr=top1000&rsv_idx=2&hisfilter=1";
        System.out.println(url.contains("homepage"));   //包含
        System.out.println(url.contains("netease"));
        String url2="D:/web/homework/logo.jpg";
        System.out.println(url2.endsWith(".png") || url2.endsWith(".jpg")); //以...结尾
        System.out.println(url2.startsWith("D:/") || url2.startsWith("d:/")); //以...开始

        String url3="https://www.baidu.com/news/baiduNewsToday/AbeShinzo.html";
        System.out.println(url3.indexOf("baidu"));  //返回某字符或某子字符串在当前字符串内第一次出现的下标(12)
        System.out.println(url3.indexOf("baidu",12+"baidu".length()));  //返回某字符或某子字符串在当前字符串内
                                                                                    //指定位置之后第一次出现的下标(从12+"baidu".length()开始,27)

        //练习:统计"ahellobhellochellohellohello"中"hello"出现了多少次
        String ss="ahellobhellochellohellohello";
        int index=-1,findStartIndex=0,counts=0;
        String find="hello";
        while ((index=ss.indexOf(find,findStartIndex))!=-1){
            counts++;
            findStartIndex=index+find.length();
        }
        System.out.println("hello共出现:"+counts+"次");
    }

}
